package com.hkedou.yuanhuo.adminsystem.business.user.controller;

import com.alibaba.fastjson.JSON;
import com.hkedou.common.view.ResultModel;
import com.hkedou.yuanhuo.adminsystem.business.user.mapper.SysUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;

@Controller
public class LoginController {

    Logger logger= LoggerFactory.getLogger("com.hkedou.adminsystem.login");


    @Resource
    SysUserMapper sysUserMapper;

    @RequestMapping(value = "/login")
    @ResponseBody
    public String login(@RequestParam(value = "loginid",required = false)String loginid,
                        @RequestParam(value = "password",required = false)String password, HttpServletRequest request, HttpServletResponse response)
    {

        ResultModel result=new ResultModel();
        result.setStatus(1);
        if(StringUtils.isEmpty(loginid)||StringUtils.isEmpty(password)){
            result.setStatus(0);
            return JSON.toJSONString(result);
        }
        logger.debug("--------------login start");
        //添加用户认证信息
        Subject subject = SecurityUtils.getSubject();
        logger.debug("--------------login subject");
        UsernamePasswordToken token = new UsernamePasswordToken(loginid,password);
        try {
            logger.debug("--------------login subject.login start");
            subject.login(token);
            Serializable id = subject.getSession().getId();
            logger.debug("jsessionid="+id);
            logger.debug("--------------login subject.login end");
        }catch (AuthenticationException e){
//            logger.error("",e);
            result.setStatus(0);
            return JSON.toJSONString(result);
        }





        return JSON.toJSONString(result);
    }

    @RequestMapping(value = "/logout")
    @ResponseBody
    public String logout()
    {

        ResultModel result=new ResultModel();
        result.setStatus(1);
        //添加用户认证信息
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.logout();
        }catch (AuthenticationException e){
            logger.error("",e);
            result.setStatus(0);
        }
        return JSON.toJSONString(result);
    }


    @RequestMapping(value="/hello",method = RequestMethod.GET)
    @ResponseBody
    public String hello(){
        Subject subject = SecurityUtils.getSubject();
        Serializable id = subject.getSession().getId();
        logger.debug("jsessionid="+id);
        return JSON.toJSONString("asus");
    }


    @RequestMapping(value="/test",method = RequestMethod.GET)
    @ResponseBody
    public ResultModel test(){
        ResultModel resultModel = new ResultModel();
        resultModel.setData("过滤器测试，不会拦截吧");
        return resultModel;
    }

}
